<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>公告管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">公告标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="titile" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">是否已读</label>
                            <div class="layui-input-inline">
                                <select name="readFlag">
                                    <option value="">全部</option>
                                    <option value="0">未读</option>
                                    <option value="1">已读</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-normal" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="button" class="layui-btn layui-btn-normal" id="data-reset-btn"><i class="layui-icon layui-icon-refresh"></i> 重 置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-xs layui-btn-normal" lay-event="markAllRead">全部标记已读</button>
                <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="deleteBatch">批量删除</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer', 'jquery'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        // 初始化表格
        table.render({
            elem: '#currentTableId',
            url: '/sysAnnouncement/queryPage',
            parseData: function (res) {
                return {
                    "code": res.code === 0 ? 0 : 1,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.list
                };
            },
            toolbar: '#toolbarDemo',
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: '序号', sort: true},
                {field: 'titile', width: 200, title: '标题'},
                {field: 'msgContent', width: 400, title: '内容'},
                {field: 'sendTime', width: 160, title: '发布时间'},
                {field: 'readFlag', width: 100, title: '阅读状态', templet: function(d) {
                        return d.readFlag == 0 ? '<span class="layui-badge layui-bg-orange">未读</span>' : '<span class="layui-badge layui-bg-green">已读</span>';
                    }},
                {title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 20, 50],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 搜索监听
        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: { curr: 1 },
                where: data.field
            });
            return false;
        });

        // 重置按钮点击事件
        $('#data-reset-btn').on('click', function () {
            $('input[name="titile"]').val('');
            $('select[name="readFlag"]').val('');
            form.render('select');

            table.reload('currentTableId', {
                page: { curr: 1 },
                where: {}
            });
        });

        // 工具栏监听
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'markAllRead') {
                layer.confirm('确定将所有消息标记为已读？', function(index) {
                    $.post('/sysAnnouncement/all', function(res) {
                        if (res.code === 0) {
                            layer.msg('全部标记成功');
                            table.reload('currentTableId');
                        } else {
                            layer.msg('操作失败: ' + res.msg);
                        }
                    });
                    layer.close(index);
                });
            }else if (obj.event === 'deleteBatch') {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;

                if (data.length === 0) {
                    layer.msg("请选择要删除的数据");
                    return;
                }

                let ids = data.map(function(item) {
                    if (item.readFlag == 0) {
                        layer.msg('存在未读消息，请先阅读后再删除');
                        throw new Error("存在未读公告");
                    }
                    return item.id;
                });

                layer.confirm('确定删除选中的消息？', function(index) {
                    $.ajax({
                        url: '/sysAnnouncement/deleteAll',
                        type: 'POST',
                        contentType: 'application/json',
                        data: JSON.stringify({ ids: ids }),
                        success: function(res) {
                            if (res.code === 0) {
                                layer.msg('删除成功');
                                table.reload('currentTableId');
                            } else {
                                layer.msg('删除失败: ' + res.msg);
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });

// 表格行操作监听
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                if (data.readFlag == 0) {
                    layer.msg('消息未读，请先阅读后再删除');
                    return;
                }
                layer.confirm('确定删除该消息吗？', function(index) {
                    $.ajax({
                        url: '/sysAnnouncement/delete?id=' + data.id,
                        type: 'GET',
                        success: function(res) {
                            if (res.code === 0) {
                                layer.msg('删除成功');
                                table.reload('currentTableId');
                            } else {
                                layer.msg('删除失败: ' + res.msg);
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });


    });
</script>
</body>
</html>
